package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.CostomFlag;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/17.
 */
public class CustomPage_GV_Adapter extends BaseAdapter {
    private Context context;
    private List<?> list;
    private LayoutInflater layoutInflater;
    int x=10;
    public CustomPage_GV_Adapter(Context context, List<?> list) {
        this.context = context;
        this.list = list;
        layoutInflater=LayoutInflater.from(context);
    }

    public int getX(){
        return x;
    }

    public void UpData(List<?>list,int x){
        this.list=list;
        this.x=x;
        this.notifyDataSetChanged();
    }

    public void UpData(){
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
       // return list==null?x:list.size();
        return CostomFlag.list_select.size()+1;
    }

    @Override
    public Object getItem(int i) {
        return CostomFlag.list_select.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Custom_GV_Holder holder;
        if(view==null){
            view=layoutInflater.inflate(R.layout.custom_gv_item,viewGroup,false);
            holder=new Custom_GV_Holder();
            holder.custom_already_iv= (ImageView) view.findViewById(R.id.custom_already_iv);
            holder.custom_already_tv= (TextView) view.findViewById(R.id.custom_already_tv);
            holder.custom_already_del= (ImageView) view.findViewById(R.id.custom_already_del);
            AutoUtils.autoSize(view);
            view.setTag(holder);
        }else{
            holder= (Custom_GV_Holder) view.getTag();
        }
       /* if(i==x-1){
            holder.custom_already_iv.setImageResource(R.mipmap.custom_add);
            holder.custom_already_tv.setText("");
            holder.custom_already_del.setVisibility(View.GONE);
        }else{
            holder.custom_already_iv.setImageResource(Constants.images[i]);
            holder.custom_already_tv.setText(Constants.names[i]);
            holder.custom_already_del.setVisibility(View.VISIBLE);
        }*/
        if(i==CostomFlag.list_select.size()){
            holder.custom_already_iv.setImageResource(R.mipmap.custom_add);
            holder.custom_already_tv.setText("");
            holder.custom_already_del.setVisibility(View.GONE);
        }else{
            holder.custom_already_iv.setImageResource(CostomFlag.list_select.get(i).getImage());
            holder.custom_already_tv.setText(CostomFlag.list_select.get(i).getName());
            holder.custom_already_del.setVisibility(View.VISIBLE);
        }

        holder.custom_already_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CostomFlag.list_select.remove(i);
                UpData();
               // UpData(null,x);
            }
        });
        return view;
    }

    class Custom_GV_Holder{
        ImageView custom_already_iv,custom_already_del;
        TextView custom_already_tv;
    }
}
